package fragments;

import by.bucha.tabnavigationproject.R;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListFragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Toast;

public class FirstList2Fragment extends SherlockListFragment implements
		OnClickListener {

	public FirstList2Fragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		showActionBar();
		String[] values = new String[] { "Android2", "iPhone2",
				"WindowsMobile2", "Blackberry2", "WebOS2", "Ubuntu2",
				"Windows72", "Max OS X2", "Linux2", "OS/22" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
	}

	private void showActionBar() {
		LayoutInflater inflator = (LayoutInflater) getSherlockActivity()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflator.inflate(R.layout.test_sherlock_actionbar_items, null);
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setCustomView(v);

		setOnClickListenerToItems(actionBar.getCustomView());
	}

	private void setOnClickListenerToItems(View v) {

		ImageButton buttonLeft = (ImageButton) v.findViewById(R.id.btnleft);

		ImageButton buttonCenter = (ImageButton) v.findViewById(R.id.btncenter);

		ImageButton buttonRight = (ImageButton) v.findViewById(R.id.btnright);

		buttonLeft.setOnClickListener(this);
		buttonCenter.setOnClickListener(this);
		buttonRight.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnleft:

			Toast.makeText(getActivity().getApplicationContext(), "LEFT",
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.btncenter:
			Toast.makeText(getActivity().getApplicationContext(), "CENTER",
					Toast.LENGTH_SHORT).show();
			System.out.println("CENTER");
			break;
		case R.id.btnright:
			Toast.makeText(getActivity().getApplicationContext(), "RIGHT",
					Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}
